﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Toenda.Foundation {
	/// <summary>
	/// Struct UriParameter
	/// </summary>
	public struct UriParameter {
		private string _key;
		private string _value;

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Initializes a new instance of the <see cref="UriParameter"/> struct.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="value">The value.</param>
		public UriParameter(string key, string value) {
			this._key = key;
			this._value = value;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UriParameter"/> struct.
		/// </summary>
		/// <param name="parameter">The parameter.</param>
		public UriParameter(UriParameter parameter) {
			this._key = parameter.Key;
			this._value = parameter.Value;
		}

		// ---------------------------------------------------
		// INTERFACE IMPLEMENTATIONS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		/// <summary>
		/// Gets the key.
		/// </summary>
		/// <value>The key.</value>
		public string Key {
			get { return this._key; }
		}

		/// <summary>
		/// Gets the value.
		/// </summary>
		/// <value>The value.</value>
		public string Value {
			get { return this._value; }
		}

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROTECTED MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Performs an implicit conversion from <see cref="System.String"/> to <see cref="UriParameter"/>.
		/// </summary>
		/// <param name="parameter">The parameter.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator UriParameter(string parameter) {
			string[] _sep = parameter.Split(new char[] { '=' });
			string _key = "";
			string _value = "";

			if(_sep.Length > 0) {
				_key = _sep[0].Replace("?", "").Replace("&", "");
			}

			if(_sep.Length > 1) {
				_value = _sep[1].Replace("?", "").Replace("&", "");
			}

			return new UriParameter(_key, _value);
		}

		/// <summary>
		/// Returns the parameter as querystring fragment
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.String"/> containing a fully qualified type name.
		/// </returns>
		public override string ToString() {
			StringBuilder str = new StringBuilder();

			if(!this._key.IsNullOrTrimmedEmpty()) {
				str.Append("&");
			}

			if(!this._key.IsNullOrTrimmedEmpty()) {
				str.Append(this._key);
			}

			if(!this._value.IsNullOrTrimmedEmpty()) {
				str.Append("=");
				str.Append(this._value);
			}

			return str.ToString();
		}
	}
}
